import { fileURLToPath, URL } from 'node:url'
import Components from "unplugin-vue-components/vite";
import AutoImport from "unplugin-auto-import/vite";
import { VantResolver } from "unplugin-vue-components/resolvers";
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'node:path';
import autoprefixer from "autoprefixer";
//@ts-ignore
import pxtorem from "postcss-pxtorem";
// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    AutoImport({
      //那些内容需要自动引入
      imports: ["vue", "pinia", "vue-router"],
      //自动引入的地址
      dts: "src/auto-import.d.ts",
      //直接调用ElMessage  不用在引入，主要是引入无效
    }),

    Components({
      resolvers: [VantResolver()],
    }),

  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  css: {
    preprocessorOptions: {
      less: {
        modifyVars: {
          hack: `true; @import (reference) "${path.resolve(__dirname, 'src/assets/css/common.less')}";`,  // src/css/common.less 是你需要全局变量 （你定义的定义的方法 和 变量等）
        },
        javascriptEnabled: true
      }
    },
    postcss: {
      plugins: [
        autoprefixer({
          overrideBrowserslist: [
            "Android 4.1",
            "iOS 7.1",
            "Chrome > 31",
            "ff > 31",
            "ie >= 8",
          ],
        }),
        pxtorem({
          rootValue: 75,
          propList: ["*"],
        }),
      ],
    },
  },
  build: {
    // chunkSizeWarningLimit: 1000, // chunks 大小限制
    assetsInlineLimit: 10000, //小于多少转化为base64
    outDir: "dist-dev",
    rollupOptions: {
      output: {
        chunkFileNames: "js/[name]-[hash].js", // 分类输出
        entryFileNames: "js/[name]-[hash].js",
        assetFileNames: "[ext]/[name]-[hash].[ext]",
        manualChunks(id) {
          if (id.includes("node_modules")) {
            // 超大静态资源拆分
            // return id.toString().split('node_modules/')[1].split('/')[0].toString()
            return "rendor";
          }
        },
      },
    },
  },
})